package com.excilys.formation.project.repository.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.excilys.formation.project.entities.Client;
import com.excilys.formation.project.repository.ClientDAO;

/**
 * Implémentation de {@link ClientDAO}.
 * 
 * @author excilys
 * 
 */
@Repository
public class ClientDAOImpl implements ClientDAO {
	/**
	 * Entity Manager injecté par Spring.
	 */
	@PersistenceContext
	private EntityManager em;

	@Override
	public Client getClient(final long id) {
		return em.find(Client.class, id);
	}

	@Override
	public Client getClientByLogin(final String login) {
		Query q = em.createQuery("FROM Client c INNER JOIN FETCH c.comptes WHERE c.login = :login");
		q.setParameter("login", login);
		return (Client) q.getSingleResult();
	}
}
